library(cregg)
library(ggplot2)
library(ggpubr)
rm(list=ls())

#### Load data 

load("data.RData")

#### Formulas for models

f1 <- vote_choice ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula
f2 <- better_pol ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula
f3 <- prob_vote ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula

#### Run models by gender 

data2 <- data[is.na(data$Vgender)==F,]

data_male <- data[data$Vgender==1,]

data_female <- data[data$Vgender==2,]

acmes.vote_choice_male <- cj(data2[data2$Vgender==1 & data2$country!="Switzerland" & data2$country!="Germany",], f1, id = ~id)
acmes.better_pol_male <- cj(data2[data2$Vgender==1 & data2$country!="Switzerland" & data2$country!="Germany",], f2, id = ~id)
acmes.prob_vote_male <- cj(data2[data2$Vgender==1 & data2$country!="Switzerland" & data2$country!="Germany",], f3, id = ~id)

acmes.vote_choice_female <- cj(data2[data2$Vgender==2 & data2$country!="Switzerland" & data2$country!="Germany",], f1, id = ~id)
acmes.better_pol_female <- cj(data2[data2$Vgender==2 & data2$country!="Switzerland" & data2$country!="Germany",], f2, id = ~id)
acmes.prob_vote_female <- cj(data2[data2$Vgender==2 & data2$country!="Switzerland" & data2$country!="Germany",], f3, id = ~id)


mm.vote_choice_male <- mm(data2[data2$Vgender==1 & data2$country!="Switzerland" & data2$country!="Germany",], f1, id = ~id)
mm.better_pol_male <- mm(data2[data2$Vgender==1 & data2$country!="Switzerland" & data2$country!="Germany",], f2, id = ~id)
mm.prob_vote_male <- mm(data2[data2$Vgender==1 & data2$country!="Switzerland" & data2$country!="Germany",], f3, id = ~id)

mm.vote_choice_female <- mm(data2[data2$Vgender==2 & data2$country!="Switzerland" & data2$country!="Germany",], f1, id = ~id)
mm.better_pol_female <- mm(data2[data2$Vgender==2 & data2$country!="Switzerland" & data2$country!="Germany",], f2, id = ~id)
mm.prob_vote_female <- mm(data2[data2$Vgender==2 & data2$country!="Switzerland" & data2$country!="Germany",], f3, id = ~id)

### Create data Figure A9

acmes.vote_choice_male$gender <- "Men"
acmes.vote_choice_female$gender <- "Women"

mm.vote_choice_male$gender <- "Men"
mm.vote_choice_female$gender <- "Women"

acmes.vote_choice_gender <- rbind(acmes.vote_choice_male, acmes.vote_choice_female)
mm.vote_choice_gender <- rbind(mm.vote_choice_male, mm.vote_choice_female)

gender_data_acmes.vote_choice <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                        "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                        "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                        "Gender", "Man", "Woman",
                                                        "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                        "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                        "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                        "Social Media", "Communicates regularly", "Does not communicate often"), 2),
                                         labels_score = rep(c(13:30,1:12), 2),
                                         estimate = c(NA, acmes.vote_choice_gender$estimate[1:5],
                                                      NA, acmes.vote_choice_gender$estimate[6:10],
                                                      NA, acmes.vote_choice_gender$estimate[11:12],
                                                      NA, acmes.vote_choice_gender$estimate[13:14],
                                                      NA, acmes.vote_choice_gender$estimate[15:16],
                                                      NA, acmes.vote_choice_gender$estimate[17:18],
                                                      NA, acmes.vote_choice_gender$estimate[19:20],
                                                      NA, acmes.vote_choice_gender$estimate[21:22],
                                                      NA, acmes.vote_choice_gender$estimate[22+1:5],
                                                      NA, acmes.vote_choice_gender$estimate[22+6:10],
                                                      NA, acmes.vote_choice_gender$estimate[22+11:12],
                                                      NA, acmes.vote_choice_gender$estimate[22+13:14],
                                                      NA, acmes.vote_choice_gender$estimate[22+15:16],
                                                      NA, acmes.vote_choice_gender$estimate[22+17:18],
                                                      NA, acmes.vote_choice_gender$estimate[22+19:20],
                                                      NA, acmes.vote_choice_gender$estimate[22+21:22]),
                                         
                                         lower = c(NA, NA, acmes.vote_choice_gender$lower[2:5],
                                                   NA, NA, acmes.vote_choice_gender$lower[7:10],
                                                   NA, NA, acmes.vote_choice_gender$lower[12],
                                                   NA, NA, acmes.vote_choice_gender$lower[14],
                                                   NA, NA, acmes.vote_choice_gender$lower[16],
                                                   NA, NA, acmes.vote_choice_gender$lower[18],
                                                   NA, NA, acmes.vote_choice_gender$lower[20],
                                                   NA, NA, acmes.vote_choice_gender$lower[22],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+2:5],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+7:10],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+12],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+14],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+16],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+18],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+20],
                                                   NA, NA, acmes.vote_choice_gender$lower[22+22]),
                                         upper = c(NA, NA, acmes.vote_choice_gender$upper[2:5],
                                                   NA, NA, acmes.vote_choice_gender$upper[7:10],
                                                   NA, NA, acmes.vote_choice_gender$upper[12],
                                                   NA, NA, acmes.vote_choice_gender$upper[14],
                                                   NA, NA, acmes.vote_choice_gender$upper[16],
                                                   NA, NA, acmes.vote_choice_gender$upper[18],
                                                   NA, NA, acmes.vote_choice_gender$upper[20],
                                                   NA, NA, acmes.vote_choice_gender$upper[22],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+2:5],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+7:10],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+12],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+14],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+16],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+18],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+20],
                                                   NA, NA, acmes.vote_choice_gender$upper[22+22]),
                                         Gender = rep(c("Men", "Women"), each=30),
                                         Gender_score = rep(c(0, 3), each=30)))



gender_data_mm.vote_choice <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                     "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                     "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                     "Gender", "Man", "Woman",
                                                     "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                     "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                     "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                     "Social Media", "Communicates regularly", "Does not communicate often"), 2),
                                      labels_score = rep(c(13:30,1:12), 2),
                                      estimate = c(NA, mm.vote_choice_gender$estimate[1:5],
                                                   NA, mm.vote_choice_gender$estimate[6:10],
                                                   NA, mm.vote_choice_gender$estimate[11:12],
                                                   NA, mm.vote_choice_gender$estimate[13:14],
                                                   NA, mm.vote_choice_gender$estimate[15:16],
                                                   NA, mm.vote_choice_gender$estimate[17:18],
                                                   NA, mm.vote_choice_gender$estimate[19:20],
                                                   NA, mm.vote_choice_gender$estimate[21:22],
                                                   NA, mm.vote_choice_gender$estimate[22+1:5],
                                                   NA, mm.vote_choice_gender$estimate[22+6:10],
                                                   NA, mm.vote_choice_gender$estimate[22+11:12],
                                                   NA, mm.vote_choice_gender$estimate[22+13:14],
                                                   NA, mm.vote_choice_gender$estimate[22+15:16],
                                                   NA, mm.vote_choice_gender$estimate[22+17:18],
                                                   NA, mm.vote_choice_gender$estimate[22+19:20],
                                                   NA, mm.vote_choice_gender$estimate[22+21:22]),
                                      
                                      lower = c(NA, mm.vote_choice_gender$lower[1:5],
                                                NA, mm.vote_choice_gender$lower[6:10],
                                                NA, mm.vote_choice_gender$lower[11:12],
                                                NA, mm.vote_choice_gender$lower[13:14],
                                                NA, mm.vote_choice_gender$lower[15:16],
                                                NA, mm.vote_choice_gender$lower[17:18],
                                                NA, mm.vote_choice_gender$lower[19:20],
                                                NA, mm.vote_choice_gender$lower[21:22],
                                                NA, mm.vote_choice_gender$lower[22+1:5],
                                                NA, mm.vote_choice_gender$lower[22+6:10],
                                                NA, mm.vote_choice_gender$lower[22+11:12],
                                                NA, mm.vote_choice_gender$lower[22+13:14],
                                                NA, mm.vote_choice_gender$lower[22+15:16],
                                                NA, mm.vote_choice_gender$lower[22+17:18],
                                                NA, mm.vote_choice_gender$lower[22+19:20],
                                                NA, mm.vote_choice_gender$lower[22+21:22]),
                                      upper = c(NA, mm.vote_choice_gender$upper[1:5],
                                                NA, mm.vote_choice_gender$upper[6:10],
                                                NA, mm.vote_choice_gender$upper[11:12],
                                                NA, mm.vote_choice_gender$upper[13:14],
                                                NA, mm.vote_choice_gender$upper[15:16],
                                                NA, mm.vote_choice_gender$upper[17:18],
                                                NA, mm.vote_choice_gender$upper[19:20],
                                                NA, mm.vote_choice_gender$upper[21:22],
                                                NA, mm.vote_choice_gender$upper[22+1:5],
                                                NA, mm.vote_choice_gender$upper[22+6:10],
                                                NA, mm.vote_choice_gender$upper[22+11:12],
                                                NA, mm.vote_choice_gender$upper[22+13:14],
                                                NA, mm.vote_choice_gender$upper[22+15:16],
                                                NA, mm.vote_choice_gender$upper[22+17:18],
                                                NA, mm.vote_choice_gender$upper[22+19:20],
                                                NA, mm.vote_choice_gender$upper[22+21:22]),
                                      Gender = rep(c("Men", "Women"), each=30),
                                      Gender_score = rep(c(0, 3), each=30)))


gender_data_acmes.vote_choice$type <- "Acmes"
gender_data_mm.vote_choice$type <- "Marginal Means"

gender_data_acmes.vote_choice$labels_score2 <- as.numeric(paste0(gender_data_acmes.vote_choice$labels_score, ".", gender_data_acmes.vote_choice$Gender_score))
gender_data_mm.vote_choice$labels_score2 <- as.numeric(paste0(gender_data_mm.vote_choice$labels_score, ".", gender_data_mm.vote_choice$Gender_score))


#### Create data Figure A10

#### second 
acmes.better_pol_male$gender <- "Men"
acmes.better_pol_female$gender <- "Women"

mm.better_pol_male$gender <- "Men"
mm.better_pol_female$gender <- "Women"

acmes.better_pol_gender <- rbind(acmes.better_pol_male, acmes.better_pol_female)
mm.better_pol_gender <- rbind(mm.better_pol_male, mm.better_pol_female)

gender_data_acmes.better_pol <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                                  "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                  "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                  "Gender", "Man", "Woman",
                                                                  "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                                  "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                                  "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                                  "Social Media", "Communicates regularly", "Does not communicate often"), 2),
                                                   labels_score = rep(c(13:30,1:12), 2),
                                                   estimate = c(NA, acmes.better_pol_gender$estimate[1:5],
                                                                NA, acmes.better_pol_gender$estimate[6:10],
                                                                NA, acmes.better_pol_gender$estimate[11:12],
                                                                NA, acmes.better_pol_gender$estimate[13:14],
                                                                NA, acmes.better_pol_gender$estimate[15:16],
                                                                NA, acmes.better_pol_gender$estimate[17:18],
                                                                NA, acmes.better_pol_gender$estimate[19:20],
                                                                NA, acmes.better_pol_gender$estimate[21:22],
                                                                NA, acmes.better_pol_gender$estimate[22+1:5],
                                                                NA, acmes.better_pol_gender$estimate[22+6:10],
                                                                NA, acmes.better_pol_gender$estimate[22+11:12],
                                                                NA, acmes.better_pol_gender$estimate[22+13:14],
                                                                NA, acmes.better_pol_gender$estimate[22+15:16],
                                                                NA, acmes.better_pol_gender$estimate[22+17:18],
                                                                NA, acmes.better_pol_gender$estimate[22+19:20],
                                                                NA, acmes.better_pol_gender$estimate[22+21:22]),
                                                   
                                                   lower = c(NA, NA, acmes.better_pol_gender$lower[2:5],
                                                             NA, NA, acmes.better_pol_gender$lower[7:10],
                                                             NA, NA, acmes.better_pol_gender$lower[12],
                                                             NA, NA, acmes.better_pol_gender$lower[14],
                                                             NA, NA, acmes.better_pol_gender$lower[16],
                                                             NA, NA, acmes.better_pol_gender$lower[18],
                                                             NA, NA, acmes.better_pol_gender$lower[20],
                                                             NA, NA, acmes.better_pol_gender$lower[22],
                                                             NA, NA, acmes.better_pol_gender$lower[22+2:5],
                                                             NA, NA, acmes.better_pol_gender$lower[22+7:10],
                                                             NA, NA, acmes.better_pol_gender$lower[22+12],
                                                             NA, NA, acmes.better_pol_gender$lower[22+14],
                                                             NA, NA, acmes.better_pol_gender$lower[22+16],
                                                             NA, NA, acmes.better_pol_gender$lower[22+18],
                                                             NA, NA, acmes.better_pol_gender$lower[22+20],
                                                             NA, NA, acmes.better_pol_gender$lower[22+22]),
                                                   upper = c(NA, NA, acmes.better_pol_gender$upper[2:5],
                                                             NA, NA, acmes.better_pol_gender$upper[7:10],
                                                             NA, NA, acmes.better_pol_gender$upper[12],
                                                             NA, NA, acmes.better_pol_gender$upper[14],
                                                             NA, NA, acmes.better_pol_gender$upper[16],
                                                             NA, NA, acmes.better_pol_gender$upper[18],
                                                             NA, NA, acmes.better_pol_gender$upper[20],
                                                             NA, NA, acmes.better_pol_gender$upper[22],
                                                             NA, NA, acmes.better_pol_gender$upper[22+2:5],
                                                             NA, NA, acmes.better_pol_gender$upper[22+7:10],
                                                             NA, NA, acmes.better_pol_gender$upper[22+12],
                                                             NA, NA, acmes.better_pol_gender$upper[22+14],
                                                             NA, NA, acmes.better_pol_gender$upper[22+16],
                                                             NA, NA, acmes.better_pol_gender$upper[22+18],
                                                             NA, NA, acmes.better_pol_gender$upper[22+20],
                                                             NA, NA, acmes.better_pol_gender$upper[22+22]),
                                                   Gender = rep(c("Men", "Women"), each=30),
                                                   Gender_score = rep(c(0, 3), each=30)))



gender_data_mm.better_pol <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                               "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                               "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                               "Gender", "Man", "Woman",
                                                               "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                               "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                               "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                               "Social Media", "Communicates regularly", "Does not communicate often"),2),
                                                labels_score = rep(c(13:30,1:12), 2),
                                                estimate = c(NA, mm.better_pol_gender$estimate[1:5],
                                                             NA, mm.better_pol_gender$estimate[6:10],
                                                             NA, mm.better_pol_gender$estimate[11:12],
                                                             NA, mm.better_pol_gender$estimate[13:14],
                                                             NA, mm.better_pol_gender$estimate[15:16],
                                                             NA, mm.better_pol_gender$estimate[17:18],
                                                             NA, mm.better_pol_gender$estimate[19:20],
                                                             NA, mm.better_pol_gender$estimate[21:22],
                                                             NA, mm.better_pol_gender$estimate[22+1:5],
                                                             NA, mm.better_pol_gender$estimate[22+6:10],
                                                             NA, mm.better_pol_gender$estimate[22+11:12],
                                                             NA, mm.better_pol_gender$estimate[22+13:14],
                                                             NA, mm.better_pol_gender$estimate[22+15:16],
                                                             NA, mm.better_pol_gender$estimate[22+17:18],
                                                             NA, mm.better_pol_gender$estimate[22+19:20],
                                                             NA, mm.better_pol_gender$estimate[22+21:22]),
                                                
                                                lower = c(NA, mm.better_pol_gender$lower[1:5],
                                                          NA, mm.better_pol_gender$lower[6:10],
                                                          NA, mm.better_pol_gender$lower[11:12],
                                                          NA, mm.better_pol_gender$lower[13:14],
                                                          NA, mm.better_pol_gender$lower[15:16],
                                                          NA, mm.better_pol_gender$lower[17:18],
                                                          NA, mm.better_pol_gender$lower[19:20],
                                                          NA, mm.better_pol_gender$lower[21:22],
                                                          NA, mm.better_pol_gender$lower[22+1:5],
                                                          NA, mm.better_pol_gender$lower[22+6:10],
                                                          NA, mm.better_pol_gender$lower[22+11:12],
                                                          NA, mm.better_pol_gender$lower[22+13:14],
                                                          NA, mm.better_pol_gender$lower[22+15:16],
                                                          NA, mm.better_pol_gender$lower[22+17:18],
                                                          NA, mm.better_pol_gender$lower[22+19:20],
                                                          NA, mm.better_pol_gender$lower[22+21:22]),
                                                upper = c(NA, mm.better_pol_gender$upper[1:5],
                                                          NA, mm.better_pol_gender$upper[6:10],
                                                          NA, mm.better_pol_gender$upper[11:12],
                                                          NA, mm.better_pol_gender$upper[13:14],
                                                          NA, mm.better_pol_gender$upper[15:16],
                                                          NA, mm.better_pol_gender$upper[17:18],
                                                          NA, mm.better_pol_gender$upper[19:20],
                                                          NA, mm.better_pol_gender$upper[21:22],
                                                          NA, mm.better_pol_gender$upper[22+1:5],
                                                          NA, mm.better_pol_gender$upper[22+6:10],
                                                          NA, mm.better_pol_gender$upper[22+11:12],
                                                          NA, mm.better_pol_gender$upper[22+13:14],
                                                          NA, mm.better_pol_gender$upper[22+15:16],
                                                          NA, mm.better_pol_gender$upper[22+17:18],
                                                          NA, mm.better_pol_gender$upper[22+19:20],
                                                          NA, mm.better_pol_gender$upper[22+21:22]),
                                                Gender = rep(c("Men", "Women"), each=30),
                                                Gender_score = rep(c(0, 3), each=30)))


gender_data_acmes.better_pol$type <- "Acmes"
gender_data_mm.better_pol$type <- "Marginal Means"


gender_data_acmes.better_pol$labels_score2 <- as.numeric(paste0(gender_data_acmes.better_pol$labels_score, ".", gender_data_acmes.better_pol$Gender_score))
gender_data_mm.better_pol$labels_score2 <- as.numeric(paste0(gender_data_mm.better_pol$labels_score, ".", gender_data_mm.better_pol$Gender_score))


#### Create data Figure A11

acmes.prob_vote_male$gender <- "Men"
acmes.prob_vote_female$gender <- "Women"

mm.prob_vote_male$gender <- "Men"
mm.prob_vote_female$gender <- "Women"

acmes.prob_vote_gender <- rbind(acmes.prob_vote_male, acmes.prob_vote_female)
mm.prob_vote_gender <- rbind(mm.prob_vote_male, mm.prob_vote_female)

gender_data_acmes.prob_vote <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                                 "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                 "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                 "Gender", "Man", "Woman",
                                                                 "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                                 "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                                 "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                                 "Social Media", "Communicates regularly", "Does not communicate often"), 2),
                                                  labels_score = rep(c(13:30,1:12), 2),
                                                  estimate = c(NA, acmes.prob_vote_gender$estimate[1:5],
                                                               NA, acmes.prob_vote_gender$estimate[6:10],
                                                               NA, acmes.prob_vote_gender$estimate[11:12],
                                                               NA, acmes.prob_vote_gender$estimate[13:14],
                                                               NA, acmes.prob_vote_gender$estimate[15:16],
                                                               NA, acmes.prob_vote_gender$estimate[17:18],
                                                               NA, acmes.prob_vote_gender$estimate[19:20],
                                                               NA, acmes.prob_vote_gender$estimate[21:22],
                                                               NA, acmes.prob_vote_gender$estimate[22+1:5],
                                                               NA, acmes.prob_vote_gender$estimate[22+6:10],
                                                               NA, acmes.prob_vote_gender$estimate[22+11:12],
                                                               NA, acmes.prob_vote_gender$estimate[22+13:14],
                                                               NA, acmes.prob_vote_gender$estimate[22+15:16],
                                                               NA, acmes.prob_vote_gender$estimate[22+17:18],
                                                               NA, acmes.prob_vote_gender$estimate[22+19:20],
                                                               NA, acmes.prob_vote_gender$estimate[22+21:22]),
                                                  
                                                  lower = c(NA, NA, acmes.prob_vote_gender$lower[2:5],
                                                            NA, NA, acmes.prob_vote_gender$lower[7:10],
                                                            NA, NA, acmes.prob_vote_gender$lower[12],
                                                            NA, NA, acmes.prob_vote_gender$lower[14],
                                                            NA, NA, acmes.prob_vote_gender$lower[16],
                                                            NA, NA, acmes.prob_vote_gender$lower[18],
                                                            NA, NA, acmes.prob_vote_gender$lower[20],
                                                            NA, NA, acmes.prob_vote_gender$lower[22],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+2:5],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+7:10],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+12],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+14],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+16],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+18],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+20],
                                                            NA, NA, acmes.prob_vote_gender$lower[22+22]),
                                                  upper = c(NA, NA, acmes.prob_vote_gender$upper[2:5],
                                                            NA, NA, acmes.prob_vote_gender$upper[7:10],
                                                            NA, NA, acmes.prob_vote_gender$upper[12],
                                                            NA, NA, acmes.prob_vote_gender$upper[14],
                                                            NA, NA, acmes.prob_vote_gender$upper[16],
                                                            NA, NA, acmes.prob_vote_gender$upper[18],
                                                            NA, NA, acmes.prob_vote_gender$upper[20],
                                                            NA, NA, acmes.prob_vote_gender$upper[22],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+2:5],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+7:10],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+12],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+14],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+16],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+18],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+20],
                                                            NA, NA, acmes.prob_vote_gender$upper[22+22]),
                                                  Gender = rep(c("Men", "Women"), each=30),
                                                  Gender_score = rep(c(0, 3), each=30)))



gender_data_mm.prob_vote <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                              "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                              "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                              "Gender", "Man", "Woman",
                                                              "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                              "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                              "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                              "Social Media", "Communicates regularly", "Does not communicate often"),2),
                                               labels_score = rep(c(13:30, 1:12), 2),
                                               estimate = c(NA, mm.prob_vote_gender$estimate[1:5],
                                                            NA, mm.prob_vote_gender$estimate[6:10],
                                                            NA, mm.prob_vote_gender$estimate[11:12],
                                                            NA, mm.prob_vote_gender$estimate[13:14],
                                                            NA, mm.prob_vote_gender$estimate[15:16],
                                                            NA, mm.prob_vote_gender$estimate[17:18],
                                                            NA, mm.prob_vote_gender$estimate[19:20],
                                                            NA, mm.prob_vote_gender$estimate[21:22],
                                                            NA, mm.prob_vote_gender$estimate[22+1:5],
                                                            NA, mm.prob_vote_gender$estimate[22+6:10],
                                                            NA, mm.prob_vote_gender$estimate[22+11:12],
                                                            NA, mm.prob_vote_gender$estimate[22+13:14],
                                                            NA, mm.prob_vote_gender$estimate[22+15:16],
                                                            NA, mm.prob_vote_gender$estimate[22+17:18],
                                                            NA, mm.prob_vote_gender$estimate[22+19:20],
                                                            NA, mm.prob_vote_gender$estimate[22+21:22]),
                                               
                                               lower = c(NA, mm.prob_vote_gender$lower[1:5],
                                                         NA, mm.prob_vote_gender$lower[6:10],
                                                         NA, mm.prob_vote_gender$lower[11:12],
                                                         NA, mm.prob_vote_gender$lower[13:14],
                                                         NA, mm.prob_vote_gender$lower[15:16],
                                                         NA, mm.prob_vote_gender$lower[17:18],
                                                         NA, mm.prob_vote_gender$lower[19:20],
                                                         NA, mm.prob_vote_gender$lower[21:22],
                                                         NA, mm.prob_vote_gender$lower[22+1:5],
                                                         NA, mm.prob_vote_gender$lower[22+6:10],
                                                         NA, mm.prob_vote_gender$lower[22+11:12],
                                                         NA, mm.prob_vote_gender$lower[22+13:14],
                                                         NA, mm.prob_vote_gender$lower[22+15:16],
                                                         NA, mm.prob_vote_gender$lower[22+17:18],
                                                         NA, mm.prob_vote_gender$lower[22+19:20],
                                                         NA, mm.prob_vote_gender$lower[22+21:22]),
                                               upper = c(NA, mm.prob_vote_gender$upper[1:5],
                                                         NA, mm.prob_vote_gender$upper[6:10],
                                                         NA, mm.prob_vote_gender$upper[11:12],
                                                         NA, mm.prob_vote_gender$upper[13:14],
                                                         NA, mm.prob_vote_gender$upper[15:16],
                                                         NA, mm.prob_vote_gender$upper[17:18],
                                                         NA, mm.prob_vote_gender$upper[19:20],
                                                         NA, mm.prob_vote_gender$upper[21:22],
                                                         NA, mm.prob_vote_gender$upper[22+1:5],
                                                         NA, mm.prob_vote_gender$upper[22+6:10],
                                                         NA, mm.prob_vote_gender$upper[22+11:12],
                                                         NA, mm.prob_vote_gender$upper[22+13:14],
                                                         NA, mm.prob_vote_gender$upper[22+15:16],
                                                         NA, mm.prob_vote_gender$upper[22+17:18],
                                                         NA, mm.prob_vote_gender$upper[22+19:20],
                                                         NA, mm.prob_vote_gender$upper[22+21:22]),
                                               Gender = rep(c("Men", "Women"), each=30),
                                               Gender_score = rep(c(0, 3), each=30)))


gender_data_acmes.prob_vote$type <- "Acmes"
gender_data_mm.prob_vote$type <- "Marginal Means"


gender_data_acmes.prob_vote$labels_score2 <- as.numeric(paste0(gender_data_acmes.prob_vote$labels_score, ".", gender_data_acmes.prob_vote$Gender_score))
gender_data_mm.prob_vote$labels_score2 <- as.numeric(paste0(gender_data_mm.prob_vote$labels_score, ".", gender_data_mm.prob_vote$Gender_score))


# Plot Figure A9

png("Figures/Figure A9.png", width = 2500, height = 3000, res=300)
ggplot(rbind(gender_data_acmes.vote_choice, gender_data_mm.vote_choice))+
  geom_point(aes(x=estimate, y=labels_score2, color = Gender))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = Gender, y=labels_score2), height = 0.2)+
  scale_y_continuous(breaks = c(1:30), labels = unique(as.character(gender_data_acmes.vote_choice$labels))[c(19:30,1:18)],
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue"))+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", 
        panel.border = element_rect(colour = "black", fill=NA, linewidth=.5), 
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off()

# Plot Figure A10

png("Figures/Figure A10.png", width = 2500, height = 3000, res=300)
ggplot(rbind(gender_data_acmes.better_pol, gender_data_mm.better_pol))+
  geom_point(aes(x=estimate, y=labels_score2, color = Gender))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = Gender, y=labels_score2), height = 0.2)+
  scale_y_continuous(breaks = c(1:30), labels = unique(as.character(gender_data_acmes.better_pol$labels))[c(19:30,1:18)],
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue"))+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", 
        panel.border = element_rect(colour = "black", fill=NA, linewidth=.5), 
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off()


# Plot Figure A11

png("Figures/Figure A11.png", width = 2500, height = 3000, res=300)
ggplot(rbind(gender_data_acmes.prob_vote, gender_data_mm.prob_vote))+
  geom_point(aes(x=estimate, y=labels_score2, color = Gender))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = Gender, y=labels_score2), height = 0.2)+
  scale_y_continuous(breaks = c(1:30), labels = unique(as.character(gender_data_acmes.prob_vote$labels))[c(19:30,1:18)],
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue"))+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", 
        panel.border = element_rect(colour = "black", fill=NA, linewidth=.5), 
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off()

